/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.votedata.votecheck.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.votedata.votecheck.entity.VoteCheck;
import com.jeeplus.modules.votedata.votecheck.mapper.VoteCheckMapper;
import com.jeeplus.modules.votedata.votecheck.entity.VoteCheckContent;
import com.jeeplus.modules.votedata.votecheck.mapper.VoteCheckContentMapper;

/**
 * 考评指标Service
 * @author 刘明星
 * @version 2021-11-18
 */
@Service
@Transactional(readOnly = true)
public class VoteCheckService extends CrudService<VoteCheckMapper, VoteCheck> {

	@Autowired
	private VoteCheckContentMapper voteCheckContentMapper;

	public VoteCheck get(String id) {
		VoteCheck voteCheck = super.get(id);
		voteCheck.setVoteCheckContentList(voteCheckContentMapper.findList(new VoteCheckContent(voteCheck)));
		return voteCheck;
	}

	public List<VoteCheck> findList(VoteCheck voteCheck) {
		return super.findList(voteCheck);
	}

	public Page<VoteCheck> findPage(Page<VoteCheck> page, VoteCheck voteCheck) {
		return super.findPage(page, voteCheck);
	}

	@Transactional(readOnly = false)
	public void save(VoteCheck voteCheck) {
		super.save(voteCheck);
		for (VoteCheckContent voteCheckContent : voteCheck.getVoteCheckContentList()){
			if (voteCheckContent.getId() == null){
				continue;
			}
			if (VoteCheckContent.DEL_FLAG_NORMAL.equals(voteCheckContent.getDelFlag())){
				if (StringUtils.isBlank(voteCheckContent.getId())){
					voteCheckContent.setCheck(voteCheck);
					voteCheckContent.preInsert();
					voteCheckContentMapper.insert(voteCheckContent);
				}else{
					voteCheckContent.preUpdate();
					voteCheckContentMapper.update(voteCheckContent);
				}
			}else{
				voteCheckContentMapper.delete(voteCheckContent);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(VoteCheck voteCheck) {
		super.delete(voteCheck);
		voteCheckContentMapper.delete(new VoteCheckContent(voteCheck));
	}

}